﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using LHS.Core.Authority;
using MS.Core.Utils;
using MS.Core.Base.Transaction;
using MS.Core.Exception;
using LHS.Core.TableInfo;

namespace LHS.Win
{
    public partial class frmDangNhap : Form
    {
        // Methods
        public frmDangNhap(frmMain parent)
        {
            this._parent = parent;
            this.InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this._parent.mnuBaoCao.Enabled = false;
            this._parent.mnuNhanVien.Enabled = false;
            this._parent.mnuCapQuyen.Enabled = false;
            this._parent.mnuThayDoi.Enabled = false;
            this._parent.mnuLapBenhAn.Enabled = false;
            this._parent.mnuDangNhap.Enabled = true;
            this._parent.mnuDoiMatKhau.Enabled = false;
            this._parent.btnBaoCao.Enabled = false;
            this._parent.btnLapBenhAnMoi.Enabled = false;
            this._parent.btnTimHSBenhAn.Enabled = false;
            this._parent.btnSearchasID.Enabled = false;
            this._parent.mnuTienIch.Enabled = false;
            this._parent.mnuHoaDon.Enabled = false;
            base.Close();
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
           if (Checker.CheckBlankValue(this.tbUserName.Text))
            {
                MessageBox.Show("Vui lòng nhập tên Đăng Nhập!", "Lỗi Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.tbUserName.Focus();
            }
            else if (Checker.CheckBlankValue(this.tbPassword.Text))
            {
                MessageBox.Show("Vui lòng nhập Mật Khẩu!", "Lỗi Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.tbPassword.Focus();
            }
            else
            {
                try
                {
                    AuthorityController objAuthController = new AuthorityController();
                    DataTable dt = objAuthController.LoginSystem(tbUserName.Text, this.tbPassword.Text);

                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("Tên Đăng Nhập hoặc Mật Khẩu không hợp lệ!", "Lỗi Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        this.count++;
                        if (this.count == 3)
                        {
                            MessageBox.Show("Bạn đã đăng nhập 3 lần không hợp lệ \n", "Đăng nhập không hợp lệ", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            Application.Exit();
                        }
                    }
                    else
                    {
                        this.user = this.tbUserName.Text;
                        DataRow dr = dt.Rows[0];
                        string strRole = dr[InfoUsers.ROLISN].ToString();
                        if (strRole == "0") /// toàn quyền = admin 0 
                        {
                            this._parent.mnuBS.Enabled = true;
                            this._parent.mnuCapQuyen.Enabled = true;
                            this._parent.mnuQuyen.Enabled = true;
                            this._parent.mnuNhanVien.Enabled = true;
                            this._parent.mnuThuoc.Enabled = true;
                            this._parent.mnuXoaBenhAn.Enabled = true;
                            this._parent.mnuHD.Enabled = true;
                            this._parent.mnuDangNhap.Enabled = true;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = true;
                            this._parent.mnuLapToaThuoc.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = true;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = true;
                            this._parent.ctmCuoiThang.Enabled = true;
                            this._parent.ctmTheoBacSi.Enabled = true;
                            this._parent.btnTimHSBenhAn.Enabled = true;
                            this._parent.mnuTienIch.Enabled = true;
                            this._parent.mnuHoaDon.Enabled = true;
                            this._parent.mnuBaoCao.Enabled = true;
                            this._parent.mnuNhanVien.Enabled = true;
                            this._parent.mnuCapQuyen.Enabled = true;
                            this._parent.mnuDangNhap.Enabled = false;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = true;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = true;
                            this._parent.btnLapBenhAnMoi.Enabled = true;
                            this._parent.btnTimHSBenhAn.Enabled = true;
                            this._parent.btnSearchasID.Enabled = true;
                            this._parent.mnuTienIch.Enabled = true;
                            this._parent.mnuHoaDon.Enabled = true;
                            this._parent.mnuXayDungBieuMau.Enabled = true;
                            this._parent.menuSetUpTienCan.Enabled = true; // edit by thaoht 20091216
                            UserInformation.usrRole = "1";
                        }
                        else if (strRole == "1") //bac sy
                        {
                            this._parent.mnuBS.Enabled = true;
                            this._parent.mnuCapQuyen.Enabled = true;
                            this._parent.mnuQuyen.Enabled = true;
                            this._parent.mnuNhanVien.Enabled = true;
                            this._parent.mnuThuoc.Enabled = true;
                            this._parent.mnuXoaBenhAn.Enabled = true;
                            this._parent.mnuHD.Enabled = true;
                            this._parent.mnuDangNhap.Enabled = true;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = true;
                            this._parent.mnuLapToaThuoc.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = true;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = true;
                            this._parent.ctmCuoiThang.Enabled = true;
                            this._parent.ctmTheoBacSi.Enabled = true;
                            this._parent.btnTimHSBenhAn.Enabled = true;
                            this._parent.mnuTienIch.Enabled = true;
                            this._parent.mnuHoaDon.Enabled = true;
                            this._parent.mnuBaoCao.Enabled = false;
                            this._parent.mnuNhanVien.Enabled = false;
                            this._parent.mnuCapQuyen.Enabled = false;
                            this._parent.mnuDangNhap.Enabled = false;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = true;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = false;
                            this._parent.btnLapBenhAnMoi.Enabled = true;
                            this._parent.btnTimHSBenhAn.Enabled = true;
                            this._parent.btnSearchasID.Enabled = true;
                            this._parent.mnuTienIch.Enabled = true;
                            this._parent.mnuHoaDon.Enabled = false;
                            this._parent.mnuXayDungBieuMau.Enabled = true;
                            this._parent.menuSetUpTienCan.Enabled = true; // edit by thaoht 20091216
                            UserInformation.usrRole = "1";
                        }
                        else if (strRole == "10") //yta 2-->10
                        {
                            this._parent.mnuBS.Enabled = true;
                            this._parent.mnuCapQuyen.Enabled = true;
                            this._parent.mnuQuyen.Enabled = true;
                            this._parent.mnuNhanVien.Enabled = true;
                            this._parent.mnuThuoc.Enabled = true;
                            this._parent.mnuXoaBenhAn.Enabled = true;
                            this._parent.mnuHD.Enabled = true;
                            this._parent.mnuDangNhap.Enabled = true;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = true;
                            this._parent.mnuLapToaThuoc.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = true;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = true;
                            this._parent.ctmCuoiThang.Enabled = true;
                            this._parent.ctmTheoBacSi.Enabled = true;
                            this._parent.btnTimHSBenhAn.Enabled = true;
                            this._parent.mnuTienIch.Enabled = true;
                            this._parent.mnuHoaDon.Enabled = true;
                            this._parent.mnuBaoCao.Enabled = false;
                            this._parent.mnuNhanVien.Enabled = false;
                            this._parent.mnuCapQuyen.Enabled = false;
                            this._parent.mnuDangNhap.Enabled = false;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = true;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = false;
                            this._parent.btnLapBenhAnMoi.Enabled = true;
                            this._parent.btnTimHSBenhAn.Enabled = true;
                            this._parent.btnSearchasID.Enabled = true;
                            this._parent.mnuTienIch.Enabled = false;
                            this._parent.mnuHoaDon.Enabled = false;
                            this._parent.mnuXayDungBieuMau.Enabled = false;
                            this._parent.menuSetUpTienCan.Enabled = false; // edit by thaoht 20091216
                            UserInformation.usrRole = "10";
                        }
                        else //dược sỹ 3-->9
                        {
                            this._parent.mnuBaoCao.Enabled = true;
                            this._parent.mnuBS.Enabled = false;
                            this._parent.mnuCapQuyen.Enabled = true;
                            this._parent.mnuQuyen.Enabled = false;
                            this._parent.mnuNhanVien.Enabled = false;
                            this._parent.mnuThuoc.Enabled = true;
                            this._parent.mnuXoaBenhAn.Enabled = false;
                            this._parent.mnuHD.Enabled = false;
                            this._parent.mnuDangNhap.Enabled = false;
                            this._parent.mnuThayDoi.Enabled = true;
                            this._parent.mnuLapBenhAn.Enabled = true;
                            this._parent.mnuLapBenhAnMoi.Enabled = false;
                            this._parent.mnuLapToaThuoc.Enabled = true;
                            this._parent.mnuTimHSBenhAn.Enabled = false;
                            this._parent.mnuDoiMatKhau.Enabled = true;
                            this._parent.btnBaoCao.Enabled = true;
                            this._parent.ctmCuoiThang.Enabled = true;
                            this._parent.ctmTheoBacSi.Enabled = false;
                            this._parent.btnLapBenhAnMoi.Enabled = false;
                            this._parent.btnTimHSBenhAn.Enabled = false;
                            this._parent.btnSearchasID.Enabled = false;
                            this._parent.mnuTienIch.Enabled = false;
                            this._parent.mnuHoaDon.Enabled = true;
                            this._parent.mnuXayDungBieuMau.Enabled = false;
                            this._parent.menuSetUpTienCan.Enabled = false; // edit by thaoht 20091216
                            UserInformation.usrRole = "9";
                        }

                        Class.name = this.user;
                        Class.isn = dr[InfoUsers.USRISN].ToString();
                        base.Close();
                    }
                }
                catch (Exception ex)
                {
                    Class.ShowException(ex);
                }
            }
        }

        private void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btLogin_Click(sender, e);
            }
        }

        private void tbUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btLogin_Click(sender, e);
            }
        }

        private void frmDangNhap_Load(object sender, EventArgs e)
        {
            //this.btLogin_Click(sender, e);
        }
    }
}